import json

from django.http import JsonResponse
from rest_framework.views import APIView

from apps.app01 import models


class RePasswordView(APIView):
    """重置密码"""

    authentication_classes = []

    def post(self, request):
        id = request.query_params['id']
        reg_dict = json.loads(request.body.decode())
        if reg_dict['password'] != reg_dict['con_password']:
            return JsonResponse({'code': 400, 'message': '密码与确认密码不符'}, status=400)
        for i in reg_dict['password']:
            if u"\u4e00" < i < u"\u9fff" or not reg_dict['password'].isalnum():
                return JsonResponse({'code': 402, 'message': '密码中含有特殊字符'}, status=402)
        user = models.User.objects.filter(id=id)
        user.update(password=reg_dict['password'])
        return JsonResponse({'code': 200, 'message': 'OK'}, status=200)